//
//  SearchData.m
//  扫码
//
//  Created by 鲍志刚 on 16/4/11.
//  Copyright © 2016年 鲍志刚. All rights reserved.
//

#import "SearchData.h"
#import "UserData.h"
#import "NetworkTools.h"

@implementation SearchData

-(void)setValue:(id)value forUndefinedKey:(NSString *)key {}

- (instancetype)initWithDict:(NSDictionary *)dict {
   
    
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
        
        NSMutableArray *mutable = [NSMutableArray array];
        
        for (NSDictionary *dict in self.data) {
            
            UserData *userData = [UserData datasWithDict:dict];
            
            [mutable addObject:userData];
        }
        
        
        self.data = mutable;
        
    }
    
    return self;
    
    
}

+ (instancetype)searchDataWithDict:(NSDictionary *)dict {
    
    return [[self alloc] initWithDict:dict];
 
}


+ (void)searchDatasWithURL:(NSString *)strUrl successBlock:(void (^)(NSArray *))successBlock errorBlock:(void (^)(NSError *))errorBlock {
    
    [[NetworkTools sharedTools] GET:strUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        
        NSDictionary *temp = responseObject;
        
        NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:10];

        
        for (NSDictionary *dcit in temp) {
            
            SearchData *searchData = [self searchDataWithDict:dcit];
            
            [mArray addObject:searchData];
            
        }
        
        if (successBlock) {
            successBlock(mArray.copy);
        }
        
        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (errorBlock) {
            errorBlock(error);
        }

    }];
  
}

@end
